package cn.jbolt.common.util;

import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;

/**
 * Created by 100147 on 2017/5/4.
 */
public class AESUtils {
	//两种常用的AES加密算法
    public static final String CIPHER_CODE="AES/CBC/PKCS7Padding";
    public static final String CIPHER_CODE2="AES/CBC/PKCS5Padding";

    /**
     * AES加密
     * @param content 需要加密的内容 *
     * @param key 加密密码 *
     * @return
     */
    public static String encrypt(String content, String key) {
    	return encrypt(content, key,null,CIPHER_CODE);
    }
    /**
     * AES加密
     * @param content 需要加密的内容 
     * @param key 加密密码 
     * @param secretIV 16位随机字符串
     * @return
     */
    public static String encrypt(String content,String key,String secretIV) {
    	return encrypt(content, key,secretIV,CIPHER_CODE);
    	
    }    

    /**
     * AES加密
     * @param content 需要加密的内容 
     * @param key 加密密码 
     * @param secretIV 16位随机字符串
     * @param cipherCode 加密算法
     * @return
     */
    public static String encrypt(String content,String key,String secretIV,String cipherCode) {
        try {
        	byte[] iv = null;
        	//有IV生成特定加密字符串
        	if(StrKit.notBlank(secretIV)) {        		
        		iv = secretIV.getBytes("UTF-8");
        	}else {
        	//没有IV生成随机加密字符串	
        		iv = StringUtils.random(16, StringUtils.RandomType.ALL).getBytes("UTF-8");
        	}
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance(cipherCode);// 创建密码器
            byte[] byteContent = content.getBytes("UTF-8");
            cipher.init(Cipher.ENCRYPT_MODE, keySpec,ivSpec);// 初始化
            byte[] result = cipher.doFinal(byteContent);
            return Base64.encodeBase64String((ByteUtils.byteMerger(iv,result)));
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return "";
    }
    /**
     * AES解密
     * @param content  待解密内容
     * @param key 解密密钥
     * @return
     */
    public static String decrypt(String content, String key) {
        try {
            byte [] cotentByte = Base64.decodeBase64(content);
            byte [] iv = new byte[16];
            byte [] aesContentByte = new byte[cotentByte.length-16];
            System.arraycopy(cotentByte,0,iv,0,16);
            System.arraycopy(cotentByte,16,aesContentByte,0,aesContentByte.length);
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance(CIPHER_CODE);// 创建密码器
            cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);// 初始化
            byte[] result = cipher.doFinal(aesContentByte);
            return new String(result); //
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String decrypt(String content, String key,String cipherCode){
        try {
            byte [] cotentByte = Base64.decodeBase64(content);
            byte [] iv = new byte[16];
            byte [] aesContentByte = new byte[cotentByte.length-16];
            System.arraycopy(cotentByte,0,iv,0,16);
            System.arraycopy(cotentByte,16,aesContentByte,0,aesContentByte.length);
            SecretKeySpec keySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
            IvParameterSpec ivSpec = new IvParameterSpec(iv);
            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
            Cipher cipher = Cipher.getInstance(cipherCode);// 创建密码器
            cipher.init(Cipher.DECRYPT_MODE,keySpec,ivSpec);// 初始化
            byte[] result = cipher.doFinal(aesContentByte);
            return new String(result); //
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static void main(String[] args) throws Exception {
        String aesKey = "asdfghjklzxcvbnm";
        JSONObject params = new JSONObject();
        params.put("platform_code","WWYC001");
        params.put("number","000430104001002");
        String q=encrypt(params.toJSONString(), aesKey);
        System.out.println(q);
        String o =decrypt(q,aesKey);
        System.out.println(o);
        JSONObject obj=JSONObject.parseObject(o);
        System.out.println(obj.getString("number"));



    }



}
